package cc.orange.mainView;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;

import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityWebViewBinding;
//import com.tendcloud.tenddata.TalkingDataSDK;

public class WebViewActivity extends BaseActivity {

    private String mUrl;
    private boolean isRead;
    private CountDownTimer countDownTimer;
    private boolean cancel = false;

    private ActivityWebViewBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
        initView();
    }

    protected void initView() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) binding.rlTop.getLayoutParams();
        params.setMargins(0, getStatusBarHeight(), 0, 0);
        binding.rlTop.setLayoutParams(params);
        WebSettings ws = binding.web.getSettings();
        // 网页内容的宽度是否可大于WebView控件的宽度
        ws.setLoadWithOverviewMode(true);
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(false);
        ws.setBuiltInZoomControls(false);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        ws.setDatabaseEnabled(true);
        ws.setAllowFileAccess(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // setDefaultZoom  api19被弃用
        // 设置此属性，可任意比例缩放。
        ws.setUseWideViewPort(true);
        ws.setTextZoom(180);
//        ws.setUseWideViewPort(true);
//        ws.setAppCacheEnabled();
        // 缩放比例 1
//        webView.setInitialScale(1);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
//        //  页面加载好以后，再放开图片
//        ws.setBlockNetworkImage(true);
        // 使用localStorage则必须打开
//        ws.setDomStorageEnabled(true);
//        ws.setCacheMode();
//        ws.setLoadsImagesAutomatically(true); // 加载图片
        // 排版适应屏幕
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // WebView是否支持多个窗口。
        ws.setSupportMultipleWindows(true);
        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        ws.setJavaScriptCanOpenWindowsAutomatically(true);
        WebView.setWebContentsDebuggingEnabled(true);
        binding.web.setWebChromeClient(new WebChromeClient() {
                                   @Override
                                   public void onReceivedTitle(WebView view, String title) {
                                       super.onReceivedTitle(view, title);
                                   }

                                   @Override
                                   public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                                       return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
                                   }
                               }
        );
        binding.web.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
        countDownTimer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                cancel = true;
            }
        };

        binding.stepBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.web.canGoBack()) {
                    binding.web.goBack();
                } else {
                    finish();
                }
            }
        });

        initData();
    }


    protected void initData() {
        isRead = getIntent().getBooleanExtra("isRead", false);
        mUrl = getIntent().getStringExtra("url");
        binding.tvWebTitle.setText(getIntent().getStringExtra("title"));
        if (mUrl.contains("http")) {
            binding.web.loadUrl(mUrl);
        }
    }

    //跳转
    public static void openH5Activity( Context context,
                                       String title,  String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
//        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }
}